import pygame
from globalconstants import *
from globalstates import *
from DebugLogger import *
    
class ActionBase(object):
    def __init__(self):
        self.description = "Action Base Class - not intended for usage"
        self.state_msg = "Action state msg - change this"
        self.roundTemperatureDecrease = -1;
        self.roundRadiationIncrease = -1;

    def perform(self):
        pass
        
    def react_to(self, action):
        if action == self.action:
            self._is_done = True
                                                        
    def is_visible(self):
        return True;
    
    def is_possible(self):
        return not self._is_done
    
    def get_label(self):
        if (self.is_possible()):
            return self.description + " (click to perform)"
        else:
            return self.description + " (no longer possible)"
                
    
class Action0(ActionBase):
    def __init__(self):
        self.action = ACTION_WATER_FROM_HELICOPTERS
        self.description = "Release water from helicopters."        
        self.rect = pygame.Rect(10, 100, 30, 30)
        self._is_done = False;
        self.hasBeenUsed = False;
        self.state_msg = "Helicopter is standing by."
        self.roundTemperatureDecrease = -1
        self.roundRadiationIncrease = -1
        self.roundRadiationDecrease = -1   
    def perform(self):        
        if self._is_done:                               
            if self.hasBeenUsed == False:
                if GlobalStates.Radiation_Level > 100:
                    self.state_msg = "Helicopter had to turn around due to high radiation."                
                else:                            
                    self.roundTemperatureDecrease = GlobalStates.DecreaseCoreTemperature(100)                     
                    self.state_msg = "Helicopter successfully released water on reactors."
                self.hasBeenUsed = True
            else:
                self.state_msg = "Helicopter returned home."
                self.roundTemperatureDecrease = -1                   
                            
        
class Action1(ActionBase):
    def __init__(self):
        self.action = ACTION_COOL_WITH_SEAWATER
        self.description = "Cool with sea water."
        self.rect = pygame.Rect(10, 150, 30, 30)
        self._is_done = False;        
        self.state_msg = "Cooling with seawater not yet started."
        self.effective_rounds = 3
        self.roundTemperatureDecrease = -1
        self.roundRadiationIncrease = -1
        self.roundRadiationDecrease = -1
        self.hasBeenUsed = False
        self.coolingSystemCorrodedWithSeaWater = False
    def perform(self):
        if self._is_done:
            if self.hasBeenUsed == False:      
                if self.effective_rounds == 3:
                    self.state_msg = "Cooling with seawater.."
                    self.roundTemperatureDecrease = GlobalStates.DecreaseCoreTemperature(10) 
                elif self.effective_rounds == 2:
                    self.state_msg = "Cooling with seawater.."
                    self.roundTemperatureDecrease = GlobalStates.DecreaseCoreTemperature(10)
                elif self.effective_rounds == 1:
                    self.state_msg = "Reactor building almost filled with seawater. Reduced cooling temperature."
                    self.roundTemperatureDecrease = GlobalStates.DecreaseCoreTemperature(5)
                else:
                    self.hasBeenUsed = True
                    self.state_msg = "Reactor building full with seawater. Had to dump radioactive water to the sea."
                    self.roundTemperatureDecrease = -1    
                    self.roundRadiationIncrease = GlobalStates.IncreaseRadiationLevel(100)
                    self.coolingSystemCorrodedWithSeaWater = True
                self.effective_rounds -= 1
            else:
                self.roundTemperatureDecrease = -1
                self.roundRadiationIncrease = -1;
                self.state_msg = "Can not be used again in order to prevent more radioactive release."
                
class Action2(ActionBase):
    def __init__(self):
        self.action = ACTION_TOKIO_FIREMEN
        self.description = "Call the tokio firemen."
        self.rect = pygame.Rect(10, 200, 30, 30)
        self._is_done = False;
        self.state_msg = "Tokyo firefighters are standing by."
        self.rounds_to_arrive = 3
        self.start_firemen_count = 10
        self.firemen_count = self.start_firemen_count
        self.roundTemperatureDecrease = -1
        self.roundRadiationIncrease = -1
        self.roundRadiationDecrease = -1
        self.haveBeenUsed = False
    def perform(self):
        if self._is_done:
            self.rounds_to_arrive -= 1
            if self.haveBeenUsed == False:
                if self.rounds_to_arrive <= 0:  
                    self.state_msg = "Tokyo firefighters arrived. " + str(self.firemen_count) +" remaining."          
                    if GlobalStates.Radiation_Level <= 200:
                        if GlobalStates.IsActionActive(ACTION_RADIATION_PROTECTION) == False:
                            self.firemen_count -= 1                    
                    elif GlobalStates.Radiation_Level > 200 and GlobalStates.Radiation_Level <= 400:
                        if GlobalStates.IsActionActive(ACTION_RADIATION_PROTECTION) == False:
                            self.firemen_count -= 4
                        else:
                            self.firemen_count -= 2
                    elif GlobalStates.Radiation_Level > 400:
                        if GlobalStates.IsActionActive(ACTION_RADIATION_PROTECTION) == False:
                            self.firemen_count -= 6
                        else:
                            self.firemen_count -= 3
        
                    if self.firemen_count <= 0:
                        self.firemen_count = 0 
                        self.haveBeenUsed = True
                        self.roundTemperatureDecrease = -1;
                        self.state_msg = "Tokyo firefighters no longer available."       
                    
                    self.roundTemperatureDecrease = GlobalStates.DecreaseCoreTemperature(self.firemen_count * 2 )
                else:
                    self.roundTemperatureDecrease = -1;
                    self.state_msg = "Tokyo firefighters called, e.t.a. in " + str(self.rounds_to_arrive) + " turns. "
                    
            else:
                self.state_msg = "Tokyo firefighters no longer available."

class Action3(ActionBase):
    def __init__(self):
        self.action = ACTION_RESTORE_MAINS_POWER
        self.description = "Restore mains power."
        self.rect = pygame.Rect(10, 250, 30, 30)
        self._is_done = False;
        self.state_msg = "Mains power down in control room and in reactor cooling."
        self.roundTemperatureDecrease = -1
        self.roundRadiationIncrease = -1
        self.roundRadiationDecrease = -1
        self.power_restore = False
        self.rounds = 2;
    def perform(self):
        if self._is_done:    
           if self.rounds > 0:
                coolingRepairState = False
                for act in GlobalStates.AllCounterMeasuresCompleteList:
                    if ACTION_REPAIR_COOLING_SYSTEM == act.action:
                        coolingRepairState = act._is_done
                        break
        
                if coolingRepairState == False:
                    self.state_msg = "Mains power restored in control room. Repairs for other areas still ongoing."
                else:
                    self.state_msg = "Mains power repairs still ongoing - getting special cable."          
                
                self.rounds -= 1;
           else:
                self.state_msg = "Mains power restored in control room."
                self.power_restore = True

class Action4(ActionBase):
    def __init__(self):
        self.action = ACTION_REPAIR_COOLING_SYSTEM
        self.description = "Repair cooling system."
        self.rect = pygame.Rect(10, 300, 30, 30)
        self._is_done = False;
        self.state_msg = "Cooling system damaged, mains power down."
        self.roundTemperatureDecrease = -1
        self.roundRadiationIncrease = -1
        self.roundRadiationDecrease = -1
    def perform(self):
        mainsState = False
        for act in GlobalStates.AllCounterMeasuresCompleteList:
            if ACTION_RESTORE_MAINS_POWER == act.action:
                mainsState = act.power_restore
                break
            
        if self._is_done:                
            if mainsState:
                
                corrodedbySeawater = False 

                for act in GlobalStates.AllCounterMeasuresCompleteList:
                    if ACTION_COOL_WITH_SEAWATER == act.action:
                        corrodedbySeawater = act.coolingSystemCorrodedWithSeaWater
                        break
                
                if corrodedbySeawater:
                    self.state_msg = "Cooling system corroded by sea water. Running with reduced power."                    
                    self.roundTemperatureDecrease = GlobalStates.DecreaseCoreTemperature(15) 
                else: 
                    self.state_msg = "Cooling system running, with full efficiency."
                    self.roundTemperatureDecrease = GlobalStates.DecreaseCoreTemperature(30) 
                
            else:
                self.state_msg = "Cooling System repaired. Mains power still down."
                                                              
        else:            
            if mainsState:
                self.state_msg = "Cooling system still damaged, mains power up."      

        
class Action5(ActionBase):
    def __init__(self):
        self.action = ACTION_RADIATION_PROTECTION
        self.description = "Equip workers with radiation protection."
        self.rect = pygame.Rect(10, 350, 30, 30)
        self._is_done = False
        self.state_msg = "Workers are unprotected."
        self.roundTemperatureDecrease = -1
        self.roundRadiationIncrease = -1
        self.roundRadiationDecrease = -1      
    def perform(self):
        if self._is_done == True:
            self.state_msg = "Workers are protected"
        
class Action6(ActionBase):
    def __init__(self):
        self.action = ACTION_FIX_VALVE_IN_COOLING_PIPES
        self.description = "Repair damage in cooling pipes."
        self.rect = pygame.Rect(10, 400, 30, 30)
        self._is_done = False;
        self.state_msg = "No damage in cooling pipes."
        self.roundTemperatureDecrease = -1
        self.roundRadiationIncrease = -1
        self.roundRadiationDecrease = -1
        self.used = False
        self.repaired = False
    def perform(self):
                            
        pipesBroken = False
        for acc in GlobalStates.AccidentsOccuredList:
            if ACCIDENT_BROKEN_PIPES_INNER_COOLING_SYSTEM == acc.accident:
                pipesBroken = True
                break
        
        if self._is_done:
            
            if self.used:
                self.state_msg = "Cooling pipe repair no longer available."
            else:
                if pipesBroken:
                    
                    self.repaired = True
                    self.state_msg = "Repaired damaged cooling pipes."
                    for acc in GlobalStates.AccidentsOccuredList:
                        if ACCIDENT_BROKEN_PIPES_INNER_COOLING_SYSTEM == acc.accident:
                            GlobalStates.AccidentsOccuredList.remove(acc)
                            break
                else:
                    self.repaired = False
                    self.used = True
                    self.state_msg = "Sent worker team to repair cooling pipes, no damages found."

        else:        
            if pipesBroken:
                self.state_msg + "Cooling pipes damaged, need repair."
        
class Action7(ActionBase):
    def __init__(self):
        self.action = ACTION_APPLY_SYNTHETIC_RESIN
        self.description = "Apply synthetic resin."
        self.rect = pygame.Rect(10, 450, 30, 30)
        self._is_done = False;
        self.state_msg = "Synthetic resin not yet applied."
        self.roundTemperatureDecrease = -1
        self.roundRadiationIncrease = -1
        self.roundRadiationDecrease = -1
    def perform(self):
        if self._is_done:
            self.state_msg = "Synthetic resin applied."
            self.roundRadiationDecrease = GlobalStates.DecreaseRadiationLevel(100)
        
class Action8(ActionBase):
    def __init__(self):
        self.action = ACTION_PLUG_LEAKS_WITH_SODIUM_SILICATE
        self.description = "Plug leaks with sodium silicate."
        self.rect = pygame.Rect(10, 500, 30, 30)
        self._is_done = False;
        self.state_msg = "Sodium silicate not yet used."
        self.roundTemperatureDecrease = -1
        self.roundRadiationIncrease = -1
        self.roundRadiationDecrease = -1
        self.reactor_leak_fixed = False
        self.used_sodium = False
    def perform(self):        
        if self._is_done:
            if self.used_sodium == False:
                reactorLeak = False            
                acc_index = 0;
                for acc in GlobalStates.AccidentsOccuredList:
                    if ACCIDENT_BROKEN_PIPES_INNER_COOLING_SYSTEM < acc.accident:
                        acc_index = acc.accident
                        reactorLeak = True
                
                if reactorLeak:
                    for acc in GlobalStates.AccidentsOccuredList:
                        if acc_index == acc.accident:
                            GlobalStates.AccidentsOccuredList.remove(acc)
                            self.state_msg = "Plugged leak with sodium silicate. Decreasing radiation output."
                            self.used_sodium = True                    
                            break;
                else:
                    self.state_msg = "Plugged leak with sodium silicate. Decreasing radiation output."
                    self.reactor_leak_fixed = False
                    self.used_sodium = True
            else:
                self.state_msg = "Sodium silicate no longer available."
        
